<template>
  <view>
    <!-- 搜索 -->
    <Search />
    <!-- 分类 -->
    <view class="category">
      <!-- 1. 顶级分类 parent -->
      <view class="sup">
        <!-- scroll-view区域滚动
        区域滚动？页面下某个盒子滚动
        页面滚动？整个页面滚动
        移动端问题：区域滚动和页面滚动冲突 =》使用scroll-view实现区域滚动
        class="active" 选中父分类需要添加的类名
         -->
        <scroll-view scroll-y>
          <text
            v-for="(parent, index) in cates"
            :class="{ active: index === selectedIndex }"
            :key="parent.cat_id"
            @click="selectParent(index)"
            >{{ parent.cat_name }}</text
          >
        </scroll-view>
      </view>
      <!-- 2. 子级分类 son -->
      <view class="sub">
        <scroll-view scroll-y>
          <!-- 封面图 -->
          <image
            src="http://static.botue.com/ugo/uploads/category.png"
            class="thumb"
          />
          <!-- 子分类列表 -->
          <view class="children" v-for="child in childCate" :key="child.cat_id">
            <view class="title">{{ child.cat_name }}</view>
            <!-- 子分类下的品牌 -->
            <view class="brands">
              <navigator
                v-for="brand in child.children"
                :key="brand.cat_id"
                :url="`/packone/list/index?query=${brand.cat_name}`"
              >
                <image :src="brand.cat_icon" />
                <text>{{ brand.cat_name }}</text>
              </navigator>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script>


export default {
  data () {
    return {
      // 商品分类数据
      cates: [],
      // 选中的父分类的索引值
      selectedIndex: 0
    }
  },
  computed: {
    childCate () {
      // 默认获取第一个父分类的子分类
      // 父分类索引值selectedIndex发生变化，计算属性函数会重新执行计算
      console.log('父分类索引值selectedIndex发生变化：', this.selectedIndex)
      return this.cates.length && this.cates[this.selectedIndex].children
    }
  },
  onLoad () {
    this.getCates()
  },
  methods: {
    // 点击父分类回调函数
    selectParent (index) {
      console.log('当前点击父分类索引：', index)
      this.selectedIndex = index
    },
    // 获取分类数据
    async getCates () {
      const { data } = await this.request({
        url: '/api/public/v1/categories'
      })
      console.log('分类数据：', data)
      this.cates = data
    }
  },
};
</script>

<style scoped lang="scss">
scroll-view {
  height: 100%;
}

.category {
  display: flex;
  width: 100%;
  position: absolute;
  top: 100rpx;
  bottom: 0;

  .sup {
    width: 196rpx;
    background-color: #f4f4f4;

    text {
      display: block;
      height: 100rpx;
      text-align: center;
      line-height: 100rpx;
      font-size: 27rpx;
      color: #333;
      border-bottom: 1rpx solid #eee;

      &:last-child {
        border-bottom: none;
      }

      &.active {
        background-color: #fff;
        color: #ea4451;
        position: relative;

        &::before {
          content: "";
          display: block;
          width: 8rpx;
          height: 60rpx;
          transform: translateY(-50%);
          background-color: #ea4451;

          position: absolute;
          left: 0;
          top: 50%;
        }
      }
    }
  }

  .sub {
    flex: 1;
    padding: 20rpx 18rpx;

    .thumb {
      width: 100%;
      height: 180rpx;
    }

    .children {
      text-align: center;
      color: #333;

      .title {
        display: inline-block;
        margin: 40rpx 0 20rpx;
        font-size: 30rpx;

        &::before {
          content: "/";
          margin-right: 20rpx;
          color: #666;
        }

        &::after {
          content: "/";
          margin-left: 20rpx;
          color: #666;
        }
      }
    }

    .brands {
      display: flex;
      flex-wrap: wrap;

      navigator {
        width: 33%;
        margin-bottom: 20rpx;
      }

      image {
        width: 120rpx;
        height: 120rpx;
      }

      text {
        display: block;
        font-size: 24rpx;
      }
    }
  }
}
</style>
